package method_injection;

import org.springframework.beans.factory.ObjectFactory;
import service.IFXNewsPersister;

/**
 * @author wx-li
 * @company 希睿科技(www.xraitech.com)
 * @copyright (c) 2018 希睿科技( www.xraitech.com )Inc. All rights reserved.
 * @date 2020/10/16 17:20
 * @since JDK1.8
 */
public class MockNewsPersister implements IFXNewsPersister/*, BeanFactoryAware*/ {
//    private BeanFactory beanFactory;
    private ObjectFactory newsBeanFactory;
    private FXNewsBean newsBean;


    public void persistNews(FXNewsBean fxNewsBean) {
        persistNews();
    }

    public void persistNews() {
        System.out.println("persist bean:" + getNewsBean());
    }

    public FXNewsBean getNewsBean() {
        // 1.通过Method Injection每次返回不同的实例(需要进行配置)
//        return newsBean;
        // 2.通过BeanFactory
//        return (FXNewsBean) beanFactory.getBean("newsBean");
        // 3.使用ObjectFactoryCreatingFactoryBean, 可以隔离客户端对象对BeanFactory的直接引用
        return (FXNewsBean) newsBeanFactory.getObject();
    }


    public void setNewsBean(FXNewsBean newsBean) {
        this.newsBean = newsBean;
    }

    public void setNewsBeanFactory(ObjectFactory newsBeanFactory) {
        this.newsBeanFactory = newsBeanFactory;
    }
}
